package com.runfast.paotui.web.controller.server;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.*;
import com.runfast.paotui.service.PaotuiCostDistanceService;
import com.runfast.paotui.service.PaotuiCostService;
import com.runfast.paotui.service.PaotuiCostTimeService;
import com.runfast.paotui.service.PaotuiCostWeightService;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentbusiness;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description: 跑腿费用模板
 * @Auther:  chenzheng
 * @Date: 2018/7/14 14:18
 **/
@RestController("paoTuiCostController")
@RequestMapping("/server/paotui/cost")
public class PaoTuiCostController {
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private PaotuiCostService paotuiCostService;
    @Autowired
    private PaotuiCostDistanceService paotuiCostDistanceService;
    @Autowired
    private PaotuiCostWeightService paotuiCostWeightService;
    @Autowired
    private PaotuiCostTimeService paotuiCostTimeService;
    /**
     * 配送费用设置列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, Integer agentId){
        Map<Object,Object> map1=new HashMap<Object, Object>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        PaotuiCostExample example=new PaotuiCostExample();
        PaotuiCostExample.Criteria exampleCriteria=example.createCriteria();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商
            if(agentId!=null){
                if(agentId.equals(agentAccounts.getAgentid())){
                    exampleCriteria.andAgentidEqualTo(agentId);
                }else{return Result.fail(null,"只能查看自己的配送信息！");}
            }else{exampleCriteria.andAgentidEqualTo(agentAccounts.getAgentid());}
        }else if(admin!=null && admin.getType()==1){
            //后台
            if(agentId!=null){
                WaiMaiRunfastAgentbusiness agentbusiness1=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(agentId);
                if(agentbusiness1!=null){
                    exampleCriteria.andAgentidEqualTo(agentbusiness1.getId());
                }else{return Result.fail(null,"找不到对应的代理商！");}
            }
        }
        List<PaotuiCost> list=paotuiCostService.selectByExampleWithPageable(example,pageable);
        if(list.size()>0){
            for(PaotuiCost paotuiCost:list){
                PaotuiCostDistanceExample example1=new PaotuiCostDistanceExample();
                example1.createCriteria().andCostidEqualTo(paotuiCost.getId());
                List<PaotuiCostDistance> paotuiCostDistanceList=paotuiCostDistanceService.selectByExample(example1);
                paotuiCost.setPaotuiCostDistanceList(paotuiCostDistanceList);

                PaotuiCostWeightExample example2=new PaotuiCostWeightExample();
                example2.createCriteria().andCostidEqualTo(paotuiCost.getId());
                List<PaotuiCostWeight> paotuiCostWeightList=paotuiCostWeightService.selectByExample(example2);
                paotuiCost.setPaotuiCostWeightList(paotuiCostWeightList);

                PaotuiCostTimeExample example3=new PaotuiCostTimeExample();
                example3.createCriteria().andCostidEqualTo(paotuiCost.getId());
                List<PaotuiCostTime> paotuiCostTimeList=paotuiCostTimeService.selectByExample(example3);
                if(paotuiCostTimeList.size()>0){
                    for(PaotuiCostTime costTime:paotuiCostTimeList){
                        setPaotuiCost1(costTime);
                    }
                }
                paotuiCost.setPaotuiCostTimeList(paotuiCostTimeList);
            }
        }
        Integer total=paotuiCostService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 配送费用设置列表
     */
   /* @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/


    /**配送模板详情*/
    @GetMapping("/info")
    public Result info(PaotuiCost paotuiCost1){
        PaotuiCost paotuiCost=new PaotuiCost();
        if(paotuiCost1!=null && paotuiCost1.getId()!=null){
            paotuiCost=paotuiCostService.selectByPrimaryKey(paotuiCost1.getId());
            PaotuiCostDistanceExample example=new PaotuiCostDistanceExample();
            example.createCriteria().andCostidEqualTo(paotuiCost.getId());
            List<PaotuiCostDistance> paotuiCostDistanceList=paotuiCostDistanceService.selectByExample(example);
            paotuiCost.setPaotuiCostDistanceList(paotuiCostDistanceList);

            PaotuiCostWeightExample example1=new PaotuiCostWeightExample();
            example1.createCriteria().andCostidEqualTo(paotuiCost.getId());
            List<PaotuiCostWeight> paotuiCostWeightList=paotuiCostWeightService.selectByExample(example1);
            paotuiCost.setPaotuiCostWeightList(paotuiCostWeightList);

            PaotuiCostTimeExample example2=new PaotuiCostTimeExample();
            example2.createCriteria().andCostidEqualTo(paotuiCost.getId());
            List<PaotuiCostTime> paotuiCostTimeList=paotuiCostTimeService.selectByExample(example2);
            if(paotuiCostTimeList.size()>0){
                for(PaotuiCostTime costTime:paotuiCostTimeList){
                    setPaotuiCost1(costTime);
                }
            }
            paotuiCost.setPaotuiCostTimeList(paotuiCostTimeList);
            if(paotuiCost==null){
                return Result.fail(null,"找不到对应的配送信息！");
            }
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null,paotuiCost);
    }
    public  void setPaotuiCost1(PaotuiCostTime costTime){
        if(costTime.getStarttime()!=null && costTime.getEndtime()!=null){
            costTime.setStartDate(TimeUtil.getHourToString(costTime.getStarttime()));
            costTime.setEndDate(TimeUtil.getHourToString(costTime.getEndtime()));
        }
    }

    /**
     * 保存配送费用设置
     */
    @PostMapping("/save")
    public Result save(@RequestBody PaotuiCost paotuiCost) throws ParseException{
        if(paotuiCost!=null && paotuiCost.getAgentid()!=null && paotuiCost.getPaotuiCostDistanceList().size()>0 && paotuiCost.getPaotuiCostWeightList().size()>0 && paotuiCost.getPaotuiCostTimeList().size()>0
                && paotuiCost.getBasictime()!=null && paotuiCost.getBasedistance()!=null && paotuiCost.getDaigoubasecost()!=null && paotuiCost.getQusongjianbasecost()!=null && paotuiCost.getOvertimedistance()!=null
                && paotuiCost.getOvertimetime()!=null){
            //setDeliverCost(deliverCost);
            WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(paotuiCost.getAgentid());
            if(agentbusiness!=null){
                paotuiCost.setAgentid(agentbusiness.getId());
                paotuiCost.setAgentname(agentbusiness.getName());
            }else{return Result.fail(null,"找不到对应的代理商！");}
            paotuiCost.setCreatetime(new Date());

            paotuiCostService.saveFor(paotuiCost);
            if(paotuiCost.getPaotuiCostDistanceList().size()>0){
                for(PaotuiCostDistance costDistance:paotuiCost.getPaotuiCostDistanceList()){
                    costDistance.setCostid(paotuiCost.getId());
                    paotuiCostDistanceService.insertSelective(costDistance);
                }
            }
            if(paotuiCost.getPaotuiCostWeightList().size()>0){
                for(PaotuiCostWeight costWeight:paotuiCost.getPaotuiCostWeightList()){
                    costWeight.setCostid(paotuiCost.getId());
                    paotuiCostWeightService.insertSelective(costWeight);
                }
            }
            if(paotuiCost.getPaotuiCostTimeList().size()>0){
                for(PaotuiCostTime costTime:paotuiCost.getPaotuiCostTimeList()){
                    setCostTime(costTime);
                    costTime.setCostid(paotuiCost.getId());
                    paotuiCostTimeService.insertSelective(costTime);
                }
            }
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }
    public  void setCostTime(PaotuiCostTime costTime) throws ParseException {
        if(costTime.getStartDate()!=null && costTime.getEndDate()!=null){
            costTime.setStarttime(TimeUtil.getHourAndMinute(costTime.getStartDate()));
            costTime.setEndtime(TimeUtil.getHourAndMinute(costTime.getEndDate()));

        }else{
            costTime.setStarttime(null);
            costTime.setEndtime(null);
        }
    }
    /**
     * 修改配送费用设置
     */
    @PostMapping("/update")
    public Result update(@RequestBody PaotuiCost paotuiCost) throws ParseException {
        if(paotuiCost!=null && paotuiCost.getId()!=null && paotuiCost.getAgentid()!=null && paotuiCost.getPaotuiCostDistanceList().size()>0 && paotuiCost.getPaotuiCostWeightList().size()>0 && paotuiCost.getPaotuiCostTimeList().size()>0
                && paotuiCost.getBasictime()!=null && paotuiCost.getBasedistance()!=null && paotuiCost.getDaigoubasecost()!=null && paotuiCost.getQusongjianbasecost()!=null && paotuiCost.getOvertimedistance()!=null
                && paotuiCost.getOvertimetime()!=null){
            //setDeliverCost(deliverCost);
            PaotuiCost deliverCost1=paotuiCostService.selectByPrimaryKey(paotuiCost.getId());
            if(deliverCost1!=null){
                WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(paotuiCost.getAgentid());
                if(agentbusiness!=null){
                    if(agentbusiness.getId().equals(deliverCost1.getAgentid())){
                        paotuiCost.setAgentid(agentbusiness.getId());
                        paotuiCost.setAgentname(agentbusiness.getName());
                        paotuiCostService.updateByPrimaryKeySelective(paotuiCost);
                        if(paotuiCost.getPaotuiCostDistanceList().size()>0){
                            PaotuiCostDistanceExample example=new PaotuiCostDistanceExample();
                            example.createCriteria().andCostidEqualTo(paotuiCost.getId());
                            paotuiCostDistanceService.deleteByExample(example);
                            for(PaotuiCostDistance costDistance:paotuiCost.getPaotuiCostDistanceList()){
                                costDistance.setCostid(paotuiCost.getId());
                                paotuiCostDistanceService.insertSelective(costDistance);
                            }
                        }
                        if(paotuiCost.getPaotuiCostWeightList().size()>0){
                            PaotuiCostWeightExample example1=new PaotuiCostWeightExample();
                            example1.createCriteria().andCostidEqualTo(paotuiCost.getId());
                            paotuiCostWeightService.deleteByExample(example1);
                            for(PaotuiCostWeight costWeight:paotuiCost.getPaotuiCostWeightList()){
                                costWeight.setCostid(paotuiCost.getId());
                                paotuiCostWeightService.insertSelective(costWeight);
                            }
                        }
                        if(paotuiCost.getPaotuiCostTimeList().size()>0){
                            PaotuiCostTimeExample example2=new PaotuiCostTimeExample();
                            example2.createCriteria().andCostidEqualTo(paotuiCost.getId());
                            paotuiCostTimeService.deleteByExample(example2);
                            for(PaotuiCostTime costTime:paotuiCost.getPaotuiCostTimeList()){
                                setCostTime(costTime);
                                costTime.setCostid(paotuiCost.getId());
                                paotuiCostTimeService.insertSelective(costTime);
                            }
                        }
                    }else{return Result.fail(null,"只能修改对应代理商的配送模板！");}
                }else{return Result.fail(null,"找不到对应的代理商！");}
            }else{return Result.fail(null,"找不到对应的配送模板！");}
        }
        return Result.ok(null);
    }

    /**
     * 删除配送费用设置
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            PaotuiCostDistanceExample example=new PaotuiCostDistanceExample();
            example.createCriteria().andCostidEqualTo(id);
            paotuiCostDistanceService.deleteByExample(example);
            PaotuiCostWeightExample example1=new PaotuiCostWeightExample();
            example1.createCriteria().andCostidEqualTo(id);
            paotuiCostWeightService.deleteByExample(example1);
            PaotuiCostTimeExample example2=new PaotuiCostTimeExample();
            example2.createCriteria().andCostidEqualTo(id);
            paotuiCostTimeService.deleteByExample(example2);
            paotuiCostService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**设置为默认*/
    @GetMapping("/setToDefault")
    public Result setToDefault(PaotuiCost deliverCost){
        if(deliverCost!=null && deliverCost.getId()!=null){
            PaotuiCost deliverCost1=paotuiCostService.selectByPrimaryKey(deliverCost.getId());
            if(deliverCost1!=null){
                if(deliverCost1.getAgentid()!=null){
                    PaotuiCostExample example=new PaotuiCostExample();
                    example.createCriteria().andAgentidEqualTo(deliverCost1.getAgentid()).andIsdefaultEqualTo(1);
                    List<PaotuiCost> deliverCostList=paotuiCostService.selectByExample(example);
                    if(deliverCostList.size()>0){
                        for(PaotuiCost deliverCost2:deliverCostList){
                            deliverCost2.setIsdefault(0);
                            paotuiCostService.updateByPrimaryKeySelective(deliverCost2);
                        }
                    }
                    deliverCost1.setIsdefault(1);
                    paotuiCostService.updateByPrimaryKeySelective(deliverCost1);
                }
            }else{return Result.fail(null,"未找到对应的配送模板！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }
}